if orders and not orders.IsNewStop then


dofile (ScriptsRootPath .. "lib\\orders.transaction.stop.activated.lua")
dofile (ScriptsRootPath .. "lib\\orders.transaction.stop.take_limit.lua")


function orders:IsNewStop(transaction)
	if type(transaction) == "table" then
		return transaction.ACTION == "NEW_STOP_ORDER"
			--and transaction.EXPIRY_DATE == "TODAY"
	end
end


function orders:IsKillStop(transaction, key)
	if type(transaction) == "table" then
		return transaction.ACTION == "KILL_STOP_ORDER"
			and transaction.ex__
			and transaction.ex__.flags
			and transaction.STOP_ORDER_KEY
			and (not key or transaction.STOP_ORDER_KEY == tostring(key))
	end
end


function orders:StopTransaction(template)
	local transaction = self:NewTransaction(template, {
		--ex__ = template.ex__,
		ACTION = "NEW_STOP_ORDER",
		STOP_ORDER_KIND = "SIMPLE_STOP_ORDER",
		OFFSET_UNITS = "PRICE_UNITS",
		--QUANTITY = template.QUANTITY,
		EXPIRY_DATE = "GTC",
		--SECCODE = template.SECCODE,
		--CLASSCODE = template.CLASSCODE,
		--CLIENT_CODE = template.CLIENT_CODE,
		--ACCOUNT = template.ACCOUNT,
	})
	
	if not transaction then return end
	
	if	template.ex__.sl
		and (template.ex__.lm or template.ex__.tp)
	then
		if (template.ex__.lm or template.ex__.tp) > template.ex__.sl then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	elseif template.ex__.flags then
		if bit.test(template.ex__.flags, 2) then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	elseif template.OPERATION then
		if template.OPERATION == "B" then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	end
	
	local p = template.ex__.sl
	if not p and template.ex__.condition_price2 and tonumber(template.ex__.condition_price2) > 0 then p = template.ex__.condition_price2 end
	if not p then p = template.ex__.condition_price end
	transaction.STOPPRICE = lib:FormatPrice(p, template.ex__.instrument)
	
	p = template.ex__.price
	if not p then p = 3 * template.ex__.sl - 2 * template.ex__.lm end
	transaction.PRICE = lib:FormatPrice(p, template.ex__.instrument)
	
	self.log("StopTransaction", transaction)
	
	return transaction
end


function orders:TakeStopTransaction(template)
	local transaction = self:NewTransaction(template, {
		--ex__ = template.ex__,
		ACTION = "NEW_STOP_ORDER",
		STOP_ORDER_KIND = "TAKE_PROFIT_AND_STOP_LIMIT_ORDER",
		MARKET_TAKE_PROFIT = "yes",
		MARKET_STOP_LIMIT = "yes",
		--OFFSET = "1",					-- price back move
		--OFFSET = "0",					-- price back move
		--OFFSET_UNITS = "PRICE_UNITS",
		--SPREAD = "1",					-- take limit price
		--SPREAD = "0",					-- take limit price
		--SPREAD_UNITS = "PRICE_UNITS",
		EXPIRY_DATE = "GTC",
	})
	
	if not transaction then return end
	
	if	template.ex__.sl
		and (template.ex__.lm or template.ex__.tp)
	then
		if (template.ex__.lm or template.ex__.tp) > template.ex__.sl then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	elseif template.ex__.flags then
		if bit.test(template.ex__.flags, 2) then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	elseif template.OPERATION then
		if template.OPERATION == "B" then
			transaction.OPERATION = "S"
		else
			transaction.OPERATION = "B"
		end
	end
	--transaction.PRICE = nil
	
	local p = template.ex__.tp
	if not p then p = template.ex__.condition_price end
	transaction.STOPPRICE = lib:FormatPrice(p, template.ex__.instrument)
	
	p = template.ex__.sl
	if not p then p = template.ex__.condition_price2 end
	transaction.STOPPRICE2 = lib:FormatPrice(p, template.ex__.instrument)
	
	self.log("TakeStopTransaction", transaction)
	
	return transaction
end


function orders:KillStopTransaction(order)
	if not order then return end
	--if not self:IsOrder(order) then return end
	
	local transaction = self:NewTransaction(order, {
		ACTION="KILL_STOP_ORDER",
		--CLASSCODE=order.class_code,
		--SECCODE=order.sec_code,
		--ACCOUNT = order.account,
		--CLIENT_CODE = order.client_code,
		--QUANTITY=tostring(int(order.qty)),
		STOP_ORDER_KEY=tostring(order.order_num),
		ex__ = {
			instrument = lib:GetInstrument(order),
			key = tostring(order.order_num),
			flags = order.flags,
			price = order.price,
			condition_price = order.condition_price,
			condition_price2 = order.condition_price2,
			co_order_num = order.co_order_num,
		}
	})
	
	--if not transaction then return end
		
	self.log("KillStopTransaction", transaction)
	
	return transaction
end


function orders:SendStop(transaction, next_transaction)
	self.log("SendStop")
	return self:sendTransaction(self:StopTransaction(transaction), next_transaction)
end


function orders:SendTakeStop(transaction, next_transaction)
	self.log("SendTakeStop")
	
	if lib:exists(lib.path.init .. "take_limit.flag") then
		local t = "new" .. lib:FormatSysdate()
		self:SaveTakeLimit(t, t)
		local stop = self:StopTransaction(transaction)
		stop.ex__.take_limit = t
		local limit = self:TakeLimitTransaction(stop)
		local sent = self:sendTransaction(stop, limit, next_transaction)
		if sent then
			return sent
		end
	end
	
	return self:sendTransaction(self:TakeStopTransaction(transaction), next_transaction)
end


function orders:KillStopOrder(order)
	if not self:IsOrder(order) then return end
	
	self.log("KillStopOrder", order)
	
	for k, v in pairs(self.transactions) do
		if self:IsKillStop(v, order.order_num) then
			self.log("KillStopOrder dublicate fail")
			return true
		end
	end
	
	local transaction = self:KillStopTransaction(order)
	
	return self:sendTransaction(transaction, self:KillOrderTransaction(self:GetTakeLimit(order)))
end


end -- of file
